package com.shop.loan.util;

import com.shop.loan.redis.RedisCache;
import com.shop.loan.redis.RedisUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import xyz.erupt.core.constant.EruptRestPath;
import xyz.erupt.core.prop.EruptProp;
import xyz.erupt.upms.constant.SessionKey;
import xyz.erupt.upms.service.EruptContextService;
import xyz.erupt.upms.service.EruptSessionService;
import xyz.erupt.upms.service.EruptUserService;
import xyz.erupt.upms.util.IpUtil;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@Slf4j
@Component
public class BlackUtil {

    @Resource
    RedisUtil redisUtil;

    @Resource
    EruptUserService eruptUserService;

    @Resource
    private EruptProp eruptProp;

    @Resource
    private EruptSessionService eruptSessionService;

    @Resource
    private EruptContextService eruptContextService;

    /**
     * 校验ip是否在黑名单中
     *
     * @param httpServletRequest
     * @return
     */
    public boolean checkIsBlacklist(HttpServletRequest httpServletRequest) {
        String requestPath = httpServletRequest.getServletPath();
        if (requestPath.contains(EruptRestPath.ERUPT_API)) {
            String requestIp = IpUtil.getIpAddr(httpServletRequest);
            String blackContent = redisUtil.hget(RedisCache.blackListByIp, requestIp);
            if (blackContent != null) {
                return true;
            }
        }
        return false;
    }

    /**
     * 校验ip是否是最后登录ip
     *
     * @param httpServletRequest
     * @return
     */
    public boolean checkIpChange(HttpServletRequest httpServletRequest) {
        String requestIp = IpUtil.getIpAddr(httpServletRequest);
        String lastIp = redisUtil.get(RedisCache.eruptUserLastLoginIp + eruptUserService.getCurrentUid());
        if (!StringUtils.hasLength(lastIp) || !StringUtils.hasLength(requestIp)) {
            return false;
        }
        if (!requestIp.trim().equals(lastIp)) {
            if (eruptProp.isRedisSession()) {
                for (String uk : SessionKey.USER_KEY_GROUP) {
                    eruptSessionService.remove(uk + eruptContextService.getCurrentToken());
                }
            }
            log.info("登录ip：{},当前ip:{}", lastIp, requestIp);
            return true;
        }
        return false;
    }


    /**
     * 校验ip所在地区是否允许访问
     *
     * @param httpServletRequest
     * @return
     */
    public boolean checkIpRegion(HttpServletRequest httpServletRequest) {
        String requestIp = IpUtil.getIpAddr(httpServletRequest);
        String addressInfo = IpUtil.getCityInfo(requestIp);
        if (addressInfo == null) {
            return true;
        }
        if (addressInfo.contains("内网IP")) {
            return true;
        }
        if (!addressInfo.startsWith("中国")) {
            return false;
        }
        if (addressInfo.contains("香港") || addressInfo.contains("台湾") || addressInfo.contains("澳门")) {
            return false;
        }
        return true;
    }

}
